using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Data.Linq;
using System.Data.Linq.Mapping;
using System.IO;
using System.IO.IsolatedStorage;
using System.Linq;
using System.Windows.Media.Imaging;
using System.Xml.Linq;
namespace INTEX.DataBase.CE
{
    public partial class Mapper
    {
    /*
      var qGPBST = from m in mtemp.GPBST select m;
       DBMapper.MapGPBST(qGPBST.ToList(), true);
       */
        public List<GPBST> MapGPBST(XElement item2, bool mergedb)
        {
            List<GPBST> result = new List<GPBST>();
            var item4 = from item3 in item2.Elements()
                        select new GPBST
                        {  
                            MAINKEY = (string)item3.Element("MAINKEY"),
                            GPBMANDANT = (string)item3.Element("GPBMANDANT"),
                            GPBPARTIE = (string)item3.Element("GPBPARTIE"),
                            GPBPARTIEKZ = (string)item3.Element("GPBPARTIEKZ"),
                            GPBLO = (string)item3.Element("GPBLO"),
                            GPBLB = (string)item3.Element("GPBLB"),
                            GPBART = (string)item3.Element("GPBART"),
                            GPBVAR = (string)item3.Element("GPBVAR"),
                            GPBFARBE = (string)item3.Element("GPBFARBE"),
                            GPBBSTKGG = (string)item3.Element("GPBBSTKGG"),
                            PHOTO = (string)item3.Element("PHOTO")
                        }; 
                        
          result = item4.ToList();
          MapGPBST(result, mergedb);
          return result;
        }
        ///DBMapper.MapGPBST(mtemp.GPBST.ToList<GPBST>(), true);        
       public void MapGPBST(List<GPBST> result, bool mergedb)
        {
          if (mergedb)
            {     
            
                int loaded = 0;
                int page = 500;
                int count = result.Count();
                while (true)
                {
                    if (count - loaded < page) 
                    {
                        page = count - loaded;
                    }
                    var query =
                        (from GPBST row in result.AsQueryable()
                         select row).Skip(loaded).Take(page);
               var rowsexist = from u in LocalDB.GPBST.ToList().AsQueryable()
                                           join uc in query.AsQueryable() on  u.MAINKEY equals  uc.MAINKEY
                                           select u;
                LocalDB.GPBST.DeleteAllOnSubmit(rowsexist);
                LocalDB.SubmitChanges();
                LocalDB.GPBST.InsertAllOnSubmit(query);
                LocalDB.SubmitChanges();
                 loaded += page;
                if (count == loaded)
                        break;
                }
            }
       }
    }
    [Table(Name = "GPBST")]
    public class GPBST : TBBASE
    {
        private string _MAINKEY = "0";
        [Column(IsPrimaryKey = true, CanBeNull = false)]
        public string MAINKEY
        {
            get
            {
                return _MAINKEY;
            }
            set
            {
                if (value != _MAINKEY)
                {
                    _MAINKEY = value;
                    NotifyPropertyChanging("MAINKEY");                 
                }
            }
        }
        private string _Photo = "YES";
        [Column]
        public string PHOTO
        {
            get
            {
                return _Photo;
            }
            set
            {
                if (value != _Photo)
                {
                    _Photo = value;
                    NotifyPropertyChanging("PHOTO");
                }
            }
        } 
        [Column(IsVersion = true)]
        private Binary _version;
   
        private string  _GPBMANDANT="0";
        [Column]
        public string GPBMANDANT
        {
            get
            {
                return _GPBMANDANT;
            }
            set
            {
                if (value != _GPBMANDANT)
                {
                     _GPBMANDANT = value;
                    NotifyPropertyChanged("GPBMANDANT");
                }
            }
        }
        private string  _GPBPARTIE="0";
        [Column]
        public string GPBPARTIE
        {
            get
            {
                return _GPBPARTIE;
            }
            set
            {
                if (value != _GPBPARTIE)
                {
                     _GPBPARTIE = value;
                    NotifyPropertyChanged("GPBPARTIE");
                }
            }
        }
        private string  _GPBPARTIEKZ="0";
        [Column]
        public string GPBPARTIEKZ
        {
            get
            {
                return _GPBPARTIEKZ;
            }
            set
            {
                if (value != _GPBPARTIEKZ)
                {
                     _GPBPARTIEKZ = value;
                    NotifyPropertyChanged("GPBPARTIEKZ");
                }
            }
        }
        private string  _GPBLO="0";
        [Column]
        public string GPBLO
        {
            get
            {
                return _GPBLO;
            }
            set
            {
                if (value != _GPBLO)
                {
                     _GPBLO = value;
                    NotifyPropertyChanged("GPBLO");
                }
            }
        }
        private string  _GPBLB="0";
        [Column]
        public string GPBLB
        {
            get
            {
                return _GPBLB;
            }
            set
            {
                if (value != _GPBLB)
                {
                     _GPBLB = value;
                    NotifyPropertyChanged("GPBLB");
                }
            }
        }
        private string  _GPBART="0";
        [Column]
        public string GPBART
        {
            get
            {
                return _GPBART;
            }
            set
            {
                if (value != _GPBART)
                {
                     _GPBART = value;
                    NotifyPropertyChanged("GPBART");
                }
            }
        }
        private string  _GPBVAR="0";
        [Column]
        public string GPBVAR
        {
            get
            {
                return _GPBVAR;
            }
            set
            {
                if (value != _GPBVAR)
                {
                     _GPBVAR = value;
                    NotifyPropertyChanged("GPBVAR");
                }
            }
        }
        private string  _GPBFARBE="0";
        [Column]
        public string GPBFARBE
        {
            get
            {
                return _GPBFARBE;
            }
            set
            {
                if (value != _GPBFARBE)
                {
                     _GPBFARBE = value;
                    NotifyPropertyChanged("GPBFARBE");
                }
            }
        }
        private string  _GPBBSTKGG="0";
        [Column]
        public string GPBBSTKGG
        {
            get
            {
                return _GPBBSTKGG;
            }
            set
            {
                if (value != _GPBBSTKGG)
                {
                     _GPBBSTKGG = value;
                    NotifyPropertyChanged("GPBBSTKGG");
                }
            }
        }
      public void ConvertToFile(IsolatedStorageFile ISO,ObservableCollection<GPBST> ListInput, string filep = "", string setguid="")
        {           
            try
            {
                string localfile = filep;
                if (filep.Trim() == "")
                {
                    if (setguid.Trim() == "")
                        setguid = Guid.NewGuid().ToString();
                    localfile = "Data\\PhoneDB\\GPBST\\GPBST" +setguid + ".xml";
                }
                string folder = "Data\\PhoneDB\\GPBST" ;
                if (!ISO.DirectoryExists(folder))
                {
                    ISO.CreateDirectory(folder);
                }
                ISO.DeleteFile(localfile);
                StreamWriter sw = new StreamWriter(ISO.CreateFile(localfile));
                XDocument xdoc = new XDocument();
                //Root Element
                XElement root = new XElement("GPBST");
                if (setguid.Trim() != "")
                {
                    root = new XElement("ITX"+setguid);
                }
                //Add root to file
                xdoc.Add(root);
                foreach (GPBST row in ListInput)
                {
                    XElement f = new XElement("GPBST");
                    XElement MAINKEY = new XElement("MAINKEY");
                     f.Add(MAINKEY);
                   
                        XElement GPBMANDANT = new XElement("GPBMANDANT");
                         GPBMANDANT.Value = row.GPBMANDANT;
                          f.Add(GPBMANDANT);
                        XElement GPBPARTIE = new XElement("GPBPARTIE");
                         GPBPARTIE.Value = row.GPBPARTIE;
                          f.Add(GPBPARTIE);
                        XElement GPBPARTIEKZ = new XElement("GPBPARTIEKZ");
                         GPBPARTIEKZ.Value = row.GPBPARTIEKZ;
                          f.Add(GPBPARTIEKZ);
                        XElement GPBLO = new XElement("GPBLO");
                         GPBLO.Value = row.GPBLO;
                          f.Add(GPBLO);
                        XElement GPBLB = new XElement("GPBLB");
                         GPBLB.Value = row.GPBLB;
                          f.Add(GPBLB);
                        XElement GPBART = new XElement("GPBART");
                         GPBART.Value = row.GPBART;
                          f.Add(GPBART);
                        XElement GPBVAR = new XElement("GPBVAR");
                         GPBVAR.Value = row.GPBVAR;
                          f.Add(GPBVAR);
                        XElement GPBFARBE = new XElement("GPBFARBE");
                         GPBFARBE.Value = row.GPBFARBE;
                          f.Add(GPBFARBE);
                        XElement GPBBSTKGG = new XElement("GPBBSTKGG");
                         GPBBSTKGG.Value = row.GPBBSTKGG;
                          f.Add(GPBBSTKGG);
                  root.Add(f);
                }
                sw.Write(xdoc);
                sw.Close();
                sw.Dispose();
            }
            catch (Exception ex)
            { 
            }
      
        }                                        
    }
}
